﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class PointGenerator : MonoBehaviour
{

    PointerEventData pd;
    GraphicRaycaster gr;
    EventSystem evt;
    RectTransform canvas;
    float x = 960;
    float y = 540;

    // Start is called before the first frame update
    void Start ()
    {
        canvas = GetComponent<RectTransform> ();
        evt = EventSystem.current;
        gr = GetComponent<GraphicRaycaster> ();
        pd = new PointerEventData (evt);
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.G))
        {
            SimTouch (ScreenCoorClickPos (new Vector2 (x, y)));
            x += 2;
            y += 1;
        }

    }

    public void SimTouch (PointerEventData ped)
    {
        var rs = new List<RaycastResult> ();
        gr.Raycast (ped, rs);
        //evt.RaycastAll(ped,rs);

        try
        {
            //Debug.Log(rs.Count);
            
            GameObject obj = Instantiate (Resources.Load<GameObject> ("Point"), canvas);
            obj.GetComponent<RectTransform> ().position = ped.position;
            Debug.Log (rs[1].gameObject.name);
        }
        catch (System.Exception) { }
    }

    public PointerEventData ScreenCoorClickPos (Vector2 v)
    {
        
        var ped = new PointerEventData (evt)
        {
            position = v
        };

        return ped;
    }
}